package view.frontend.order;

import exceptionManager.ExceptionManager;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.*;
import model.*;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderProductListPanel extends JPanel
{
    private OrderAddProduct orderAddProduct;
    private int heightAddition;

    public OrderProductListPanel(OrderAddProduct view, final Product product)
    {
        orderAddProduct = view;
        heightAddition = 0;
        if (product.getName().length() > 30)
        {
            heightAddition = 20;
        }        
        
        ProgramValue programValues = null;
        try
        {
            programValues = model.handlers.ProgramValueHandler.getCurrentProgramValueModelObject();
        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        setLayout(null);
        setBounds(0, 0, 220, 180+heightAddition);
        setBackground(DesignLayout.T_FIELD_BG_COLOR);
        setBorder(DesignLayout.T_FIELD_RED);
        
        JLabel lblProductIDAndName = new JLabel("<html><b>"+product.getId()+"</b> "+product.getName()+"</html>");
        lblProductIDAndName.setBounds(10, 10, 200, 20+heightAddition);
        lblProductIDAndName.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblProductFamily = new JLabel("<html><b>V.gruppe:</b> "+product.getProductFamily().getId()+" "+product.getProductFamily().getName()+"</html>");
        lblProductFamily.setBounds(10, 30+heightAddition, 200, 20);
        lblProductFamily.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblPurchase = new JLabel("<html>Købspris: "+product.getPurchasePrice()+" ("+ (product.getPurchasePrice() * (1 + (programValues.getVat() / 100))) +")</html>");
        lblPurchase.setBounds(10, 50+heightAddition, 200, 20);
        lblPurchase.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblSale = new JLabel("<html>Salgspris: "+product.getSalesPrice()+" ("+ (product.getSalesPrice()* (1 + (programValues.getVat() / 100))) +")</html>");
        lblSale.setBounds(10, 70+heightAddition, 200, 20);
        lblSale.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblSize = new JLabel("<html><b>Størrelse:</b> "+product.getSize()+"</html>");
        lblSize.setBounds(10, 90+heightAddition, 200, 20);
        lblSize.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblDecorated = new JLabel("<html><b>Har Dekoration: </b> "+utility.Converter.booleanToString(product.isHasDecoration())+"</html>");
        lblDecorated.setBounds(10, 110+heightAddition, 200, 20);
        lblDecorated.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblServiceProduct = new JLabel("<html><b>Serviceprodukt (multisalg)</b> "+utility.Converter.booleanToString(product.isRemainAfterSale())+"</html>");
        lblServiceProduct.setBounds(10, 130+heightAddition, 200, 20);
        lblServiceProduct.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        JLabel lblActive = new JLabel("<html><b>Produkt Aktivt: </b> "+utility.Converter.booleanToString(product.isActive())+"</html>");
        lblActive.setBounds(10, 150+heightAddition, 200, 20);
        lblActive.setFont(DesignLayout.STANDARD.deriveFont(11f));
        
        add(lblProductIDAndName);
        add(lblProductFamily);
        add(lblPurchase);
        add(lblSale);
        add(lblSize);
        add(lblDecorated);
        add(lblServiceProduct);
        add(lblActive);
        
        MouseAdapter mouseReleased;
        mouseReleased =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseReleased(MouseEvent evt)
                        {
                            OrderLine orderLine = new OrderLine(0, product, orderAddProduct.getOrder().getOrderObject(), 1, product.getSalesPrice());
                            orderAddProduct.getOrder().getOrderObject().addOrderLine(orderLine);
                            try
                            {
                                // Hvis varen ikke er en multisalgs vare, så markér den som solgt.
                                if (orderLine.getProduct().isRemainAfterSale() == false)
                                {
                                    control.ProductControl.setProductSold(product, true);
                                }
                            } 
                            catch (SQLException ex)
                            {
                                ExceptionManager.handleSQLException(ex);
                            } 
                            catch (ClassNotFoundException ex)
                            {
                                ExceptionManager.handleClassNotFoundException(ex);
                            }
                            orderAddProduct.doLookUp("");
                        }
                    }
                );
        
        MouseAdapter 
                    mouseEntered,
                    mouseExited;
        
        mouseEntered =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseEntered(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.HAND_CURSOR));
                        }
                    }
                );
        
        mouseExited =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseExited(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        }
                    }
                );
        
        this.addMouseListener(mouseEntered);
        this.addMouseListener(mouseExited);
        this.addMouseListener(mouseReleased);
    }

    public int getHeightAddition()
    {
        return heightAddition;
    }
}